package com.tr.rentcar.model.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.User;

import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ParamHandler;
import com.tr.rentcar.model.jdbc.ResultHandler;




/**
 * 用户dao
 * 
 * @author gson
 *
 */
public class UserDao implements ResultHandler<User> {

	private MySqlDb db = MySqlDb.getInstance();
	
	/**
	 * 查找所有用户	
	 * @return
	 * @throws SQLException
	 */
	public List<User> findAll() throws SQLException {
		String sql = "select * from  users ";
			 return db.executeQuery(sql,  this);
	}
	/**
	 * 根据tel查找用户
	 * 
	 * 
	 * @param tel
	 * @return
	 * @throws SQLException
	 */
	public User findByTel(String tel) throws SQLException {
		String sql = "select * from users where utel = ?";
		return db.queryUninResult(sql, new GlobalParamHandler(tel), this);
	}
	/**
	 * 通过tel的模糊查询
	 */
	public List<User> findByTelmohu(String tel) throws SQLException {
		String sql = "select * from users where utel like ?";
		return db.executeQuery(sql,new GlobalParamHandler("%"+tel+"%"),this);
	}
	/**
	 * 通过tel或姓名的模糊查寻的模糊查询
	 */
	public List<User> findByTelNamemohu(String telName) throws SQLException {
		String sql = "select * from users where utel like ? or uname like ?";
		return db.executeQuery(sql,new GlobalParamHandler("%"+telName+"%","%"+telName+"%"),this);
	}
	/**
	 * 根据id查找用户
	 * 
	 * 
	 * @param tel
	 * @return
	 * @throws SQLException
	 */
	public User findByid(int uid) throws SQLException {
		String sql = "select * from users where uid = ?";
		return db.queryUninResult(sql, new GlobalParamHandler(uid), this);
	}
	
	
	/**
	 * 通过id修改用户密码
	 * 
	 */
	public boolean xgm(String passx,int uid) throws SQLException {
		String sql = "update users set upassword=? where uid = ?";
		return db.executeUpdate(sql,  new GlobalParamHandler(passx, uid)) > 0;
	}
	
	/**
	 * 根据tel修改用户密码
	 * @param passx
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public boolean updatePassword(String tel,String password) throws SQLException {
		String sql = "update users set upassword=? where utel = ?";
		return db.executeUpdate(sql,  new GlobalParamHandler(password, tel)) > 0;
	}
	
	/**
	 * 通过id修改用户余额
	 * 
	 */
	public boolean xgmoney(Double umoney,int uid) throws SQLException {
		String sql = "update users set umoney=? where uid = ?";
		return db.executeUpdate(sql,  new GlobalParamHandler(umoney, uid)) > 0;
	}
	/**
	 * 根据uid修改用户积分(评价订单)
	 */
	public boolean xiugai(int jf,int uid) throws SQLException{
		String sql="update users set ujf=ujf+? where uid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(jf,uid))>0;
	}
	/**
	 * 根据uid修改用户积分(消费)
	 * @param jf
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public boolean updateJF(int jf,int uid) throws SQLException{
		String sql="update users set ujf=ujf-? where uid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(jf,uid))>0;
	}
	
	/**
	 * 通过id修改用户
	 * @throws SQLException 
	 * 
	 */
	public boolean xginfo(String uuserName ,String utel,String uemail,int uid) throws SQLException {
		String sql = "update users set uname = ?,utel=?,uemail=? where uid = ?";
		return db.executeUpdate(sql, new GlobalParamHandler(uemail,utel,uuserName,uid)) > 0;
	}
	/**
	 * 更新用户信息
	 * @param uname
	 * @param utel
	 * @param uemail
	 * @param ustate
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public boolean updateuser( String uname, String utel,String uemail,int ustate,Long uid) throws SQLException {
		String sql = "update  users set uname=?,utel=?,uemail=?,ustate=? where uid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(uname,utel,uemail,ustate, uid)) > 0;
	}
	
	/**
	 * 支付订单后修改用户余额
	 * @param omoney
	 * @return
	 * @throws SQLException
	 */
	public boolean updateUmoney(double omoney,int uid) throws SQLException{
		String sql="update users set umoney=umoney-? where uid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(omoney,uid)) > 0;
	}
	/**
	 * 退还押金
	 * @param omoney
	 * @return
	 * @throws SQLException
	 */
	public boolean updateMoneyAfterRent(double omoney,int uid) throws SQLException{
		String sql="update users set umoney=umoney+? where uid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(omoney,uid)) > 0;
	}
	/**
	 * 修改登录状态表的用户状态下线
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public boolean updateLoginStatus(int uid) throws SQLException{
		String sql="update user_status set us_status='0' where uid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(uid))>0;
	}
	
	/**
	 * 按年，按月，按日获取上线人数
	 * @throws SQLException 
	 */
	public long getPeopleCount(String time) throws SQLException{
		String sql="select count(*) from user_status WHERE us_date like ?";
		return (long) db.executeCountQuery(sql,new GlobalParamHandler("%"+time+"%"));
	}
	/**
	 * 注册新用户
	 * 
	 * @param user
	 * @return
	 * @throws SQLException
	 */
	public boolean save(String tel,String name,String email,String password) throws SQLException {
		String sql = "insert into users(utel,uname,uemail,upassword) values(?,?,?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(tel,name,email,password)) > 0;
	}
	
	/**
	 * 用户登录成功后插入一条登录信息
	 * @param status
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public boolean insertLoginStatus(int uid) throws SQLException{
		String sql="insert into user_status(us_status,uid) values('1',?)";
		return db.executeUpdate(sql, new GlobalParamHandler(uid))>0;
	}
	/**
	 * 获取用户此时在登录状态表的登录状态
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public Long getUpCount(int uid) throws SQLException{
		String sql="select count(*) from user_status where uid=? and us_status='1'";
		return (long) db.executeCountQuery(sql, new GlobalParamHandler(uid));
	}
	
	/**
	 * 全参数处理
	 * 
	 * @author gson
	 *
	 */
	@SuppressWarnings("unused")
	private class UpdateParamHandler implements ParamHandler {
		User user;

		UpdateParamHandler(User user) {
			this.user = user;
		}

		@Override
		public void doHander(PreparedStatement pStatement) throws SQLException {
			// 处理sql参数
			pStatement.setString(1, user.getUname());
			pStatement.setString(2, user.getUpassword());
			pStatement.setString(3, user.getUtel());
			pStatement.setString(4, user.getUemail());
			pStatement.setLong(5, user.getUid());
			pStatement.setBoolean(6, user.getUstate());
			pStatement.setDouble(7, user.getUmoney());
			pStatement.setInt(8, user.getUjf());
		}
	}
	@Override
	public User doHander(Map<String, Object> row) {
		User u = new User();
		u.setUid(Integer.parseInt(row.get("uid") + ""));
		u.setUname((String) row.get("uname"));
		u.setUpassword((String) row.get("upassword"));
		u.setUtel((String) row.get("utel"));
		u.setUemail((String) row.get("uemail"));
		u.setUmoney(Double.valueOf(row.get("umoney")+""));
		u.setUstate((Integer)row.get("ustate")>0);
		u.setUjf((Integer)row.get("ujf"));
		return u;
	}

	
}
